#include "family_tree.hpp"

//设置父节点
void SetParent(FamilyTreeNode*& root, FamilyTreeNode*& parent) {
	root->parent = parent;
	if (root->leftchild != NULL) {
		SetParent(root->leftchild, root);
	}
	if (root->rightbrother != NULL) {
		SetParent(root->rightbrother, parent);
	}
}

//打印家谱所有成员信息
void PrintAllMessage(FamilyTreeNode* root) {
	if (root != NULL) {
		PrintMessage(root);
	}
	if (root->leftchild != NULL) {
		PrintAllMessage(root->leftchild);
	}
	if (root->rightbrother != NULL) {
		PrintAllMessage(root->rightbrother);
	}
}

//打印家谱某成员信息
void PrintMessage(FamilyTreeNode* target) {
	cout << "姓名:" << target->person.name << endl;
	cout << "姓别:" << target->person.gender << endl;
	cout << "出生日期:" << target->person.birthday.year << "年" << target->person.birthday.month << "月" << target->person.birthday.day << "日" << endl;
	cout << "学历:" << target->person.education << endl;
	cout << "职业:" << target->person.occupation << endl;
	if (target->person.IsAlive == "离世") {
		cout << "逝世日期:" << target->person.deathday.year << "年" << target->person.deathday.month << "月" << target->person.deathday.day << "日" << endl;
	}
	cout << "备注:" << "家谱中第" << target->person.depth << "代人" << " , "  << target->person.remarks << endl;
	cout << endl;
	cout << endl;
}

//打印某层成员信息
void PrintDepthMessage(FamilyTreeNode* root, int depth) {
	if (root->person.depth == depth) {
		PrintMessage(root);
	}
	if (root->leftchild != NULL) {
		PrintDepthMessage(root->leftchild, depth);
	}
	if (root->rightbrother != NULL) {
		PrintDepthMessage(root->rightbrother, depth);
	}
}

// 按某成员姓名和出生日期查找信息
void SearchNameAndBirthdate(FamilyTreeNode* root, string name, Date birthday, bool& IsFind) {
	if (name == root->person.name && 
	   (birthday.day == root->person.birthday.day)&& (birthday.month == root->person.birthday.month)&& (birthday.year == root->person.birthday.year)) {
		IsFind = true;
		cout << "该成员信息为：" << endl;
		PrintMessage(root);
		if (root->parent != NULL) {
			cout << "其父亲信息:" << endl;
			PrintMessage(root->parent);
		}
		FamilyTreeNode* p = root;
		int childrennum = 0;
		if (p->leftchild != NULL) {
			cout << "其孩子" << ++childrennum << "信息:" << endl;
			p = p->leftchild;
			PrintMessage(p);
			while (p->rightbrother != NULL) {
				cout << "其孩子" << ++childrennum << "信息:" << endl;
				p = p->rightbrother;
				PrintMessage(p);
			}
		}
	}
	if (root->leftchild != NULL) {
		SearchNameAndBirthdate(root->leftchild, name, birthday, IsFind);
	}
	if (root->rightbrother != NULL) {
		SearchNameAndBirthdate(root->rightbrother, name, birthday, IsFind);
	}
}

// 按姓名和出生日期输出成员信息
void PrintMessageByNameAndBirthdate(FamilyTreeNode* root) {
	string name;
	cin >> name;
	cin.ignore();
	int year;
	int month;
	int day;
	cout << "请输入年" << endl;
	cin >> year;
	cin.ignore();
	cout << "请输入月" << endl;
	cin >> month;
	cin.ignore();
	cout << "请输入日" << endl;
	cin >> day;
	cin.ignore();
	Date birthday;
	birthday.year = year;
	birthday.month = month;
	birthday.day = day;
	bool IsFind = false;
	SearchNameAndBirthdate(root, name, birthday, IsFind);
	if (IsFind == false) {
		cout << "家谱中无此姓名和出生日期的人!" << endl;
	}
}

void ReturnTreenodeByNameAndBirthday(FamilyTreeNode *&root, string name, FamilyTreeNode *&target, Date birthday) {
	if ((root->person.name == name) && (root->person.birthday.year == birthday.year) && (root->person.birthday.month == birthday.month) && (root->person.birthday.day == birthday.day)) {
		target = root;
	}
	if (root->leftchild != NULL) {
		ReturnTreenodeByNameAndBirthday(root->leftchild, name, target, birthday);
	}
	if (root->rightbrother != NULL) {
		ReturnTreenodeByNameAndBirthday(root->rightbrother, name, target, birthday);
	}
}

//添加孩子
void InsertChild(FamilyTreeNode*& root) {
	string name, gender, education, occupation;
	Date birthday;
	cout << "请输入要添加的孩子其父亲的姓名：" << endl;
	cin >> name;
	cin.ignore();
	cout << "请输入该孩子的父亲的出生日期：" << endl;
	cin >> birthday.year;
	cin.ignore();
	cin >> birthday.month;
	cin.ignore();
	cin >> birthday.day;
	cin.ignore();
	FamilyTreeNode* person = NULL;
	ReturnTreenodeByNameAndBirthday(root, name, person, birthday);
	if (person == NULL) {
		cout << "输入该孩子的父亲的信息有误!" << endl;
		return;
	}
	FamilyTreeNode* child = new FamilyTreeNode;
	child->depth = person->depth + 1;
	cout << "请输入该成员的姓名:" << endl;
	cin >> child->person.name;
	cin.ignore();
    cout << "请输入成员性别：";
    cin >> child->person.gender;
    cin.ignore();
    cout << "请输入成员学历：";
    cin >> child->person.education;
    cin.ignore();
    cout << "请输入成员职业：";
    cin >> child->person.occupation;
    cin.ignore();
	cout << "请输入生日(年,月,日):" << endl;
	cin >> child->person.birthday.year;
	cin.ignore();
	cin >> child->person.birthday.month;
	cin.ignore();
	cin >> child->person.birthday.day;
	cin.ignore();
	int word = 1;
	cout << "该人是否在世，在世输0，已去世输1" << endl;
	int IsAlive;
	cin >> IsAlive;
	cin.ignore();
	while (word)
	{
		if (IsAlive == 0) {
			child->person.IsAlive = "在世";
			child->person.deathday.day = 0;
			child->person.deathday.month = 0;
			child->person.deathday.year = 0;
			word = 0;
		} else if (IsAlive == 1) {
			child->person.IsAlive = "离世";
			word = 0;
			cout << "请输入去世日期(年,月,日):" << endl;
			cin >> child->person.deathday.year;
			cin.ignore();
			cin >> child->person.deathday.month;
			cin.ignore();
			cin >> child->person.deathday.day;
			cin.ignore();
		} else {
			cout << "输入数据非法,请重新输入" << endl;
			cin >> IsAlive;
		}
		cout << "请输入备注" << endl;
		cin >> child->person.remarks;
		cin.ignore();
	}
	if (person->leftchild == NULL) {
		person->leftchild = child;
		child->parent = person;
		child->leftchild = NULL;
		child->rightbrother = NULL;
		child->depth = person->depth + 1;
	} else {
		FamilyTreeNode* p = person->leftchild;
		while (p->rightbrother != NULL) {
			p = p->rightbrother;			
		}
		p->rightbrother = child;
		child->parent = person;
		child->leftchild = NULL;
		child->rightbrother = NULL;
		child->depth = person->depth + 1;
	}
}

//删除某成员
void DeleteMember(FamilyTreeNode*& root) {
	string name;
	Date birthday;
	cout << "请输入该成员的姓名" << endl;
	cin >> name;
	cin.ignore();
	cout << "请输入该成员的生日" << endl;
	cin >> birthday.year;
	cin.ignore();
	cin >> birthday.month;
	cin.ignore();
	cin >> birthday.day;
	cin.ignore();
	FamilyTreeNode* person = NULL;
	ReturnTreenodeByNameAndBirthday(root, name, person, birthday);
	if (person == NULL) {
		cout << "输入信息有误!" << endl;
		return;
	}
	if (person->parent != NULL) {
		FamilyTreeNode* p = person->parent;
		if (p->leftchild == person) {
			p->leftchild = person->rightbrother;
			delete person;
			person = NULL;
		}
		else {
			FamilyTreeNode* p1 = p->leftchild;
			while (p1->rightbrother != p) {
				if (p1->rightbrother == NULL) {
        			break;
    			}
				p1 = p1->rightbrother;
			}
			p1->rightbrother = person->rightbrother;
			delete person;
			person = NULL;
		}
	}
	else {
		cout << "第一代人无法删除!" << endl;
	}
}

//修改某成员信息
void ModifyMember(FamilyTreeNode*& root) {
	string name;
	Date birthday;
	cout << "请输入该成员的姓名" << endl;
	cin >> name;
	cin.ignore();
	cout << "请输入该成员的生日" << endl;
	cin >> birthday.year;
	cin.ignore();
	cin >> birthday.month;
	cin.ignore();
	cin >> birthday.day;
	cin.ignore();
	FamilyTreeNode* person = NULL;
	ReturnTreenodeByNameAndBirthday(root, name, person, birthday);
	if (person == NULL) {
		cout << "输入信息有误!" << endl;
		return;
	}
	cout << "请按下列指引修改信息！" << endl;
	cout << "请输入姓名:" << endl;
	cin >> person->person.name;
	cin.ignore();
	cout << "请输入姓别:" << endl;
	cin >> person->person.gender;
	cin.ignore();
	cout << "请输入学历:" << endl;
	cin >> person->person.education;
	cin.ignore();
	cout << "请输入职业:" << endl;
	cin >> person->person.occupation;
	cin.ignore();
	cout << "请输入生日(年,月,日):" << endl;
	cin >> person->person.birthday.year;
	cin.ignore();
	cin >> person->person.birthday.month;
	cin.ignore();
	cin >> person->person.birthday.day;
	cin.ignore();
	person->person.depth = person->depth;
	int word = 1;
	cout << "该人是否在世，在世输0，已去世输1" << endl;
	int IsAlive;
	cin >> IsAlive;
	cin.ignore();
	while (word) {
		if (IsAlive == 0) {
			person->person.IsAlive = "在世";
			person->person.deathday.day = 0;
			person->person.deathday.month = 0;
			person->person.deathday.year = 0;
			word = 0;
		}
		else if (IsAlive == 1) {
			person->person.IsAlive = "离世";
			word = 0;
			cout << "请输入去世日期(年,月,日):" << endl;
			cin >> person->person.deathday.year;
			cin.ignore();
			cin >> person->person.deathday.month;
			cin.ignore();
			cin >> person->person.deathday.day;
			cin.ignore();
		}
		else {
			cout << "输入数据非法,请重新输入" << endl;
			cin >> IsAlive;
		}
	}
	cout << "请输入备注" << endl;
	cin >> person->person.remarks;
	cin.ignore();	
}

// 统计男女比例
void countGenderRatio(FamilyTreeNode* root, int& maleCount, int& femaleCount) {
    if (!root) {
        return;
    }
    if (root->person.gender == "男") {
        maleCount++;
    } else if (root->person.gender == "女") {
        femaleCount++;
    }
	FamilyTreeNode* leftchild = root->leftchild;
	FamilyTreeNode* rightbrother = root->rightbrother;
	if (root->leftchild != NULL) {
		countGenderRatio(leftchild, maleCount, femaleCount);
	}
	if (root->rightbrother != NULL) {
		countGenderRatio(rightbrother, maleCount, femaleCount);
	}
}

// 统计男女比例
void getGenderRatio(FamilyTreeNode* root) {
	int maleCount = 0;
	int femaleCount = 0;
	countGenderRatio(root, maleCount, femaleCount);
	cout << "男性人数：女性人数 = " << maleCount << ":" << femaleCount << endl;
}

//使用正则表达式提取年份部分
// 统计平均寿命
double getAverageAge(FamilyTreeNode* root) {
    int memberCount = 0;
    int totalAge = 0;
    countMembers(root, memberCount, totalAge);
    return (double) totalAge / memberCount;
}

// 统计家庭成员总数
void countMembers(FamilyTreeNode* root, int& count, int& totalAge) {
    if (root == nullptr) {
        return;
    }
	//if (root->person.birthday != "未知" && root->person.deathday != "未知")
	int birthYear = root->person.birthday.year;
	int deathYear = root->person.deathday.year;
	int age = deathYear - birthYear;
	totalAge += age;
    count++;
	FamilyTreeNode* leftchild = root->leftchild;
	FamilyTreeNode* rightbrother = root->rightbrother;
	if (root->leftchild != NULL) {
		countMembers(leftchild, count, totalAge);
	}
	if (root->rightbrother != NULL) {
		countMembers(rightbrother, count, totalAge);
	}
}

// 显示菜单界面
void MenuInterface() {
    cout << "----------------------------------菜单-----------------------------------------" << endl;
    cout << "1.添加家庭成员信息" << endl;
    cout << "2.修改某成员信息" << endl;
    cout << "3.删除某成员及其后代" << endl;
    cout << "4.显示家谱所有成员信息" << endl;
    cout << "5.查询第n代所有人的信息" << endl;
    cout << "6.按照姓名和出生日期查询成员信息" << endl;
    cout << "7.统计家庭成员信息" << endl;
    cout << "8.退出程序" << endl;
}